<template>
  <div style="margin-top: 20px;">
    <el-form :model="formValidate" :rules="ruleValidate" ref="formValidate" label-width="80px" class="demo-ruleForm">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="formValidate.name" style="width:300px;"></el-input>
      </el-form-item>
      <el-form-item label="角色描述" prop="desc">
        <el-input type="textarea" v-model="formValidate.desc" style="width:300px;"></el-input>
      </el-form-item>
      <el-form-item label="角色描述" prop="permission">
        <el-tree
                ref="tree"
                :data="menuData"
                show-checkbox
                node-key="id"
                :props="defaultProps"
                @check-change="choiceAll"
        >
        </el-tree>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit('formValidate')">立即创建</el-button>
        <el-button @click="handretrun()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "@/axios";
export default {
  data() {
    return {
      adminData: [],
      menuData: [],
      defaultProps: {
        children: 'children',
        label: 'title',
      },
      formValidate: {
        name: "",
        desc: "",
        permission: []
      },
      ruleValidate: {
        name: [
          {
            required: true,
            message: "角色名称不能未空！",
            trigger: "blur"
          }
        ],
        desc: [
          {
            required: true,
            message: "角色描述不能未空！",
            trigger: "blur"
          }
        ],
        permission: [
          {
            type: "array", // 添加验证类型
            required: true,
            message: "请选择分配权限！",
            trigger: "change"
          }
        ]
      }
    };
  },
  mounted() {
    this.getRoleMenu();
  },
  methods: {
    //获取选择权限
    choiceAll(data) {
      //获取所有选中节点
      const checkedNodes = this.$refs.tree.getCheckedNodes();
      this.formValidate.permission = [];
      for (var i = 0; i < checkedNodes.length; i++) {
        this.formValidate.permission.push(checkedNodes[i].id);
      }
    },
    //获取权限菜单
    getRoleMenu() {
      let vm = this;
      axios
        .post("/admin/allPermission", {})
        .then(response => {
          let status = response.status;
          let data = response.data;
          if (data.code == 200) {
            let result = data.data;
            vm.menuData = result.list;

          } else {
            vm.$message.error(data.message);
          }
        })
        .catch(error => {
          // vm.$message.error("网络或服务器异常");
        });
    },
    //提交数据
    handleSubmit(name) {
      let vm = this;
      // console.log(this.checkAllId);

      this.$refs[name].validate(valid => {
        if (valid) {
          axios
            .post("/admin/role/store", vm.formValidate)
            .then(response => {
              let status = response.status;
              let data = response.data;
              if (data.code == 200) {
                this.$message({
                  message: '添加成功!',
                  type: 'success',
                  onClose: function(msg) {
                    vm.$router.push({ name: "role_list" });
                  }
                });
              } else {
                vm.$message.error(data.message);
              }
            })
            .catch(error => {
              // vm.$message.error("网络或服务器异常");
            });
        } else {
          // vm.$message.error("Fail!");
        }
      });
    },
   //返回上一页
    handretrun() {
        this.$router.go(-1);
    }

    // handleReset(name) {
    //     this.$refs[name].resetFields();
    // }
  }
};
</script>